package com.psdi.element;

import com.psdi.Main;
import com.psdi.manager.GameLoader;
import com.psdi.physics.Collider;
import com.psdi.physics.CollisionSystem;
import com.psdi.window.GameJFrame;

import java.util.HashSet;
import java.util.Set;

public class BossBullet extends ElementObject implements Collider {
    private Vector2D direction = Vector2D.RIGHT;
    private float atk = 2;
    private int num = 0;
    private float animationTime = 0.05f;
    private float staytime = 2f;
    public BossBullet(){
        transform = new Transform();
        tag=Tag.BOSSBULLET;
    };

    public ElementObject CreatBossBullet(Position pos,Vector2D Direction,Size size) {
        this.transform = new Transform(pos,new Rotation(),new Size(90, 90));
        CollisionSystem.INSTANCE.addElement(this);
        direction = Direction;
        transform.size = size;
        transform.rotation.z = controldir();
        this.tag=Tag.BOSSBULLET;
        setImage(GameLoader.iconMap2.get(tag.toString()).get(0));
        return this;
    }
    private int controldir(){
        if (direction.equals(Vector2D.DOWN)) {
            return 90;
        } else if (direction.equals(Vector2D.LEFT)) {
            return 180;
        } else if (direction.equals(Vector2D.UP)) {
            return 270;
        }
        else return 0;
    }
    @Override
    public void update() {
        num%=5;
        // 更新图片
        setImage(GameLoader.iconMap2.get(tag.toString()).get(num));
        animationTime -= Main.DELTA_TIME;
        if(animationTime <=0){
            animationTime = 0.05f;
            num++;
        }
        staytime -= Main.DELTA_TIME;
        if(staytime <= 0){
            transform.position.add(direction.scalarMultiply(4f));
        }
        if (transform.position.x<0 || transform.position.x > GameJFrame.GAME_WIDTH ||
                transform.position.y < 0 || transform.position.y > GameJFrame.GAME_HEIGHT) {
            setAlive(false); // 如果子弹超出边界，则标记为不存活
        }
    }
    @Override
    public Size colliderSize() {
        return transform.size;
    }

    @Override
    public Position colliderPosition() {
        return transform.position;
    }

    @Override
    public Tag colliderTag() {
        return tag;
    }

    @Override
    public Set<Tag> colliderFilterTag() {
        Set<Tag> tags = new HashSet<>();
        tags.add(Tag.BOSS);
        tags.add(Tag.MAP);
        tags.add(tag);
        return tags;
    }

    @Override
    public void onCollisionStay(ElementObject other) {
        if(other instanceof Player) {
            ((Player) other).setHp(((Player) other).getHp() - atk);
            other.setAlive(((Player) other).checkCondition());
            setAlive(false); // 子弹与玩家元素碰撞时，子弹消失
        }

    }
}
